package com.sumainfor.pc.market.service;

import com.sumainfor.common.Constants;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.PageUtils;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.pc.market.dao.HousingConInfoMapper;
import com.sumainfor.pc.market.dao.HousingauditMapper;
import com.sumainfor.pc.market.dao.UtilsMapper;
import com.sumainfor.pc.market.feign.GetUserInfoFeign;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author Xfma
 * @Date 2018年11月7日
 * @Describe 工程部查询房源配置
 */

@Service
public class HousingauditService implements Serializable {

    private ReentrantLock lock = new ReentrantLock(false);

    Logger log = LoggerFactory.getLogger(HousingauditService.class);// 日志

    @Autowired
    HousingauditMapper mapper;
    @Autowired
    HousingConInfoMapper housingConInfoMapper;

    @Autowired
    GetUserInfoFeign getUserInfoFeign;

    @Autowired
    UtilsMapper utilsMapper;

    /**
     * @return result
     * @Author Xfma
     * @Date 2018年11月7日
     * @Describe 查询所有的房源归属
     */
    public JsonResult getCommNi() {
        JsonResult result = new JsonResult();
        try {
            log.info("resulr-------->" + mapper.getCommNi());
            result.put(mapper.getCommNi());
        } catch (Exception e) {
            log.info("服务器繁忙....");
            result.putFailed("服务器繁忙....");
        }
        return result;

    }


    public JsonResult getHousingByFinState() throws Exception {
        JsonResult result = new JsonResult();

        return result.put(mapper.getHousingByFinState());
    }

    public JsonResult getSysUserId(Map<String, Object> params) throws Exception {
        JsonResult result = new JsonResult();
        return result.put(mapper.getSysUserId(params));
    }


    /**
     * 改变状态
     *
     * @param params
     * @return
     * @throws Exception
     */
    public JsonResult updateHousingByFinState(Map<String, Object> params) throws Exception {
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            if (mapper.updateHousingByFinState(params) > 0) {

                result.putSuccess("操作成功");

            } else {
                result.putFailed("操作失败");
            }
        } catch (Exception e) {
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;

    }

    /**
     * 分页查询查询待审核的房源
     *
     * @param params
     * @param pager
     * @return
     * @throws Exception
     */
    public JsonResult getHousing(Map<String, Object> params, Pager pager) throws Exception {
        JsonResult result = new JsonResult();
        try {
            pager.setPagerNecessary(params, pager);
            PageUtils pageUtils = new PageUtils();
            Integer cout = mapper.getHousingCount(params);
            if (cout == 0) {
                return pageUtils.getJsonResult(new ArrayList<Map<String, Object>>(), params, cout);
            }
            List<Map<String, Object>> getCommunityList = mapper.getHousing(params);
            result = pageUtils.getJsonResult(getCommunityList, params, cout);
        } catch (Exception e) {
            log.error("异常:", e);
            result.putFailed("服务器繁忙...");
        }

        return result;

    }


    /**
     * 审核房源
     *
     * @param params
     * @return
     * @throws Exception
     */

    public JsonResult updateHousingState(Map<String, Object> params) throws Exception {
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            String sysuder = mapper.getSysUserId(params);
//            if (sysuder != null && !"".equals(sysuder)) {

                if (mapper.UpdateHousingState(params) > 0) {
                    result.putSuccess("操作成功");
                } else {
                    result.putFailed("操作失败");
                }
//            } else {
//                result.putFailed("该房源未分配管家请联系运营部");
//            }


        } catch (Exception e) {
            log.error("error" + e);
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }


    public JsonResult getProjectNameAndHousingConf(List<String> huIds) {
        return JsonResult.success(mapper.getProjectNameAndHousingConf(huIds));
    }

    /**
     * 根据房源编号 分页 模糊 条件查询房源信息
     *
     * @param huIds
     * @return
     */
    public JsonResult getCommunityList(List<String> huIds) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("houIdList", huIds);
        return JsonResult.success(mapper.getCommunityList(params));
    }

    /**
     * 按房源编号查看房源详情
     *
     * @param params
     * @return
     * @throws Exception
     */
    public JsonResult getListingDetails(Map<String, Object> params) throws Exception {
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            Map<String, Object> detmap = mapper.getListingDetails(params);//查询出房源资料


            List<String> sliat = new ArrayList<String>();
            sliat.add(detmap.get("insby").toString());
            JsonResult<List<Map<String, Object>>> res = getUserInfoFeign.getUserInfo(sliat);
            List<Map<String, Object>> data = res.getResult();
            String username = MapUtils.getString(data.get(0), "username");
            detmap.put("insby", username);

            detmap.put("deptname", getUserInfoFeign.getnamebyid(detmap.get("MiId").toString()));
            List<Map<String, Object>> listowners = mapper.getOwners(params);//查询业主资料
//            List<Map<String, Object>> listHousingCon = mapper.getHousingCon(params);//查询房源配置
//            if (listHousingCon.size() > 0) {
//                for (Map map : listHousingCon) {
//                    //按照房源配置id查询 详情
//                    List<Map<String, Object>> listmap = mapper.getConInfo(map.get("HouConId").toString());
//                    map.put("info", listmap);
//                }
//            }
            detmap.put("Owners", listowners);
            //           detmap.put("Con", listHousingCon);
            log.info("detmap--->" + detmap);
            String[] imgs;
            List<String> imlist = new ArrayList<String>();
            if (detmap.get("HouImg") != null) {
                imgs = detmap.get("HouImg").toString().split(",");
                for (int i = 0; i < imgs.length; i++) {
                    imlist.add(imgs[i].substring(imgs[i].indexOf("http")));
                }
                detmap.put("HouImg", imlist);
            } else {
                detmap.put("HouImg", new ArrayList<String>());
            }
            result.put(detmap);
        } catch (Exception e) {
            log.info("e--->" + e);
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }


    public Map<String, Object> saveCon_Info(Map<String, Object> params) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            List<Map<String, Object>> housinglist = (List<Map<String, Object>>) params.get("housing");//获取的房源id
            List<Map<String, Object>> housinginfo = (List<Map<String, Object>>) params.get("housinginfo");//
            String HouConId = null;
            Integer HouConIds = null;
            Integer rescount = null;
            int i = 0;
            int j = 0;
            for (Map<String, Object> map : housinglist) {
                i++;
                //               HouConId=mapper.getHouConId(map);
//                for (Map<String,Object> informap:housinginfo) {
//                    informap.put("HouConId",HouConId);//通过房源id获取HouConId
//                    HouConIds = mapper.getMaxHouConIds(informap);//从数据库里面查询出来最大子id
//                    if(null!=HouConIds){
//                        HouConIds=HouConIds+1;
//                    }else{
//                        HouConIds=1;
//                    }
//                    informap.put("HouConIds",HouConIds);
//                    rescount=  mapper.saveCon_Info(informap);
//                    mapper.updateIsAmend(informap);//修改状态
//                    j++;
//                }
                map.put("FinState", 3);//新增成功后需要修改房源状态
                rescount = mapper.updateHousingByFinState(map);
                mapper.updateHousingByState(map);
            }
            System.err.println("i循环了" + i);
            System.err.println("j循环了" + j);
            if (rescount > 0) {

                result.put("code", 0);//表示成功
            } else {
                result.put("code", 400);//表示失败
            }
        } catch (Exception e) {
            log.info("市场部报错--->" + e);
            result.put("code", 400);//表示失败
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }

    /**
     * 新增房源配置
     *
     * @param params
     * @return
     * @throws Exception
     */
    public JsonResult saveConInfo(Map<String, Object> params) throws Exception {
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {

            Integer HouConIds = mapper.getMaxHouConIds(params);//从数据库里面查询出来最大子id
            if (HouConIds != null) {
                HouConIds = HouConIds + 1;
            } else {
                HouConIds = 1;
            }
            params.put("HouConIds", HouConIds);
            if (mapper.saveConInfo(params) > 0) {
                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }
        } catch (Exception e) {
            log.info("e--->" + e);
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }

    /**
     * 删除某一个房源配置
     *
     * @param params
     * @return
     * @throws Exception
     */
    public JsonResult deleteConInfo(Map<String, Object> params) throws Exception {
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            if (mapper.deleteConInfo(params) > 0) {
                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }
        } catch (Exception e) {
            log.info("e--->" + e);
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }

    /**
     * 修改房源配置
     *
     * @param params
     * @return
     * @throws Exception
     */
    public JsonResult updateConInfo(Map<String, Object> params) throws Exception {
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            if (mapper.updateConInfo(params) > 0) {
                mapper.updateIsAmend(params);//修改状态
                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }
        } catch (Exception e) {
            log.info("e--->" + e);
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }

    public JsonResult getHougingbyhid(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        try {
            List<Map<String, Object>> houlist = (List<Map<String, Object>>) params.get("Hou");//先获取集合
            List<Map<String, Object>> datalist = new ArrayList<Map<String, Object>>();
            for (Map<String, Object> map : houlist) {
                Map<String, Object> getmap = mapper.getHougingbyhid(map.get("HouId").toString());
                getmap.put("ComNumber", map.get("ComNumber"));
                datalist.add(getmap);
            }
            result.put(datalist);
        } catch (Exception e) {
            result.putFailed("服务器繁忙...");
        }
        return result;
    }

    /**
     * 按条件批次id 和房源装修状态查询房源
     *
     * @param params
     * @return
     */
    public JsonResult getDecoratePlanInfoResult(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        try {
            List<Map<String, Object>> datamaolist = (List<Map<String, Object>>) params.get("datamaolist");//获取集合
            List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
            for (Map<String, Object> map : datamaolist) {
                Map<String, Object> objectMap = new HashMap<String, Object>();
                Map<String, Object> stringObjectMap = housingConInfoMapper.gettcmktHousing(map.get("HouId").toString());//首先先获取房源的集合

                objectMap.put("PlansId", map.get("PlansId"));//ziid
                objectMap.put("instm", map.get("instm"));//时间
                objectMap.put("Stdtm", map.get("Stdtm"));//起止时间
                objectMap.put("HouId", stringObjectMap.get("HouId"));//fangyuanid
                objectMap.put("ComName", stringObjectMap.get("ComName"));//所属项目
                objectMap.put("BudRom", stringObjectMap.get("BudRom"));//楼房及室号
                objectMap.put("ProHouse", stringObjectMap.get("ProHouse"));//几室几厅
                objectMap.put("RoomsNum", stringObjectMap.get("RoomsNum"));//可拆分几间
                objectMap.put("DecoType", stringObjectMap.get("DecoType"));//装修类型
                objectMap.put("CivilState", map.get("CivilState"));//土建阶段状态         1装修中   2装修完成
//                objectMap.put("FitmentState",map.get("FitmentState"));//装修阶段状态         1装修中   2装修完成
//                objectMap.put("CheckState",map.get("CheckState"));//管家验房阶段状态     1装修中   2装修完成
//                objectMap.put("IfCheckState",map.get("IfCheckState"));//管家验房是否异常     0正常  1异常
                objectMap.put("SysUserId", stringObjectMap.get("SysUserId"));//验房管家编号
                objectMap.put("IfCheck", map.get("IfCheck"));//是否通知管家验房
                objectMap.put("IfCheckState", map.get("IfCheckState"));//是否验房异常
                objectMap.put("FitmentRealityTime", map.get("FitmentRealityTime"));//实际完成时间
                log.info("--------------------------");
                log.info("FitmentRealityTime",map.get("FitmentRealityTime"));
                if (map.get("CheckRealityTime") != null) {
                    objectMap.put("CheckRealityTime", map.get("CheckRealityTime"));//实际完成时间
                } else {
                    objectMap.put("CheckRealityTime", "");//实际完成时间
                }
                if ("1".equals(map.get("CivilState").toString())) {
                    objectMap.put("state", "<span  style='color:rgba(245,166,35,1);text-decoration:underline;cursor: pointer;'>土建阶段</span>");//起止时间
                    objectMap.put("Wsubmit", 0);//不可提交验房
                } else {
                    if ("1".equals(map.get("FitmentState").toString())) {

                        if ("1".equals(map.get("IfCheckState").toString())) {
                            objectMap.put("state", "<span style='color:rgba(245,166,35,1);text-decoration:underline;cursor: pointer;'>装修阶段</span>&nbsp;<span style='background:rgba(255,53,81,1);padding:4px 5px;font-size:9px;\n" +
                                    "font-family:PingFangSC-Semibold;\n" +
                                    "font-weight:500;\n" +
                                    "color:rgba(255,255,255,1);'>异常</span>");//起止时间
                            objectMap.put("Wsubmit", 2);//再次提交
                        } else {
                            objectMap.put("state", "<span  style='color:rgba(245,166,35,1);text-decoration:underline;cursor: pointer;'>装修阶段</span>");//起止时间
                            objectMap.put("Wsubmit", 1);//提交验房
                        }


                    } else {
                        if ("1".equals(map.get("CheckState").toString())) {

                            if ("1".equals(map.get("IfCheckState").toString())) {
                                objectMap.put("state", "<span style='color:rgba(245,166,35,1);text-decoration:underline;cursor: pointer;'>验房阶段</span>&nbsp;<span style='background:rgba(255,53,81,1);padding:4px 5px;font-size:9px;\n" +
                                        "font-family:PingFangSC-Semibold;\n" +
                                        "font-weight:500;\n" +
                                        "color:rgba(255,255,255,1);'>异常</span>");//起止时间
                                objectMap.put("Wsubmit", 2);//再次提交
                            } else {
                                objectMap.put("state", "<span  style='color:rgba(245,166,35,1);text-decoration:underline;cursor: pointer;'>验房阶段</span>");//起止时间
                                objectMap.put("Wsubmit", 3);//已提交验房
                            }
                        } else if ("2".equals(map.get("CheckState").toString())) {
                            objectMap.put("state", "<span  style='color:rgba(170,205,7,1);text-decoration:underline;cursor: pointer;'>完成</span>");//起止时间
                            objectMap.put("Wsubmit", 4);//验房完成
                        }
                    }

                }
                mapList.add(objectMap);
            }
            result.put(mapList);

        } catch (Exception e) {
            log.info("E====>" + e);
            result.putFailed("服务器繁忙...");
        }
        return result;
    }

    /**
     * 查询其他费用录入
     *
     * @param params
     * @return
     */
    public JsonResult postmap(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        try {
            List<Map<String, Object>> postmap = (List<Map<String, Object>>) params.get("datalist");//获取集合
            List<Map<String, Object>> reslist = new ArrayList<Map<String, Object>>();
            for (Map<String, Object> objectMap : postmap) {
                Map<String, Object> stringObjectMap = housingConInfoMapper.gettcmktHousing(objectMap.get("HouId").toString());//首先先获取房源的集合
                Map<String, Object> resmap = new HashMap<String, Object>();
                resmap.put("PlansId", objectMap.get("PlansId"));//ziid
                resmap.put("instm", objectMap.get("instm"));//时间
                resmap.put("cou", objectMap.get("cou"));//状态 0 未录入  1 已录入一条
                resmap.put("IfStatus", objectMap.get("IfStatus"));//已确认
                Integer noStatus = Integer.parseInt(objectMap.get("cou").toString()) - Integer.parseInt(objectMap.get("IfStatus").toString());
                resmap.put("noStatus", noStatus);//未确认
                resmap.put("IfRest", objectMap.get("IfRest"));//是否修改

                resmap.put("HouId", stringObjectMap.get("HouId"));//fangyuanid
                resmap.put("ComName", stringObjectMap.get("ComName"));//所属项目
                resmap.put("BudRom", stringObjectMap.get("BudRom"));//楼房及室号
                resmap.put("ProHouse", stringObjectMap.get("ProHouse"));//几室几厅
                resmap.put("RoomsNum", stringObjectMap.get("RoomsNum"));//可拆分几间
                resmap.put("DecoType", stringObjectMap.get("DecoType"));//装修类型
                resmap.put("SysUserId", stringObjectMap.get("SysUserId"));//管家验房编号
                resmap.put("IfCheck", objectMap.get("IfCheck"));//是否通知管家验房
                objectMap.put("IfCheckState", objectMap.get("IfCheckState"));//是否验房异常
                if (objectMap.get("CheckRealityTime") != null) {
                    resmap.put("CheckRealityTime", objectMap.get("CheckRealityTime"));//实际完成时间
                } else {
                    resmap.put("CheckRealityTime", "");//实际完成时间

                }
                if ("1".equals(objectMap.get("CivilState").toString())) {
                    resmap.put("Wsubmit", 0);//不可提交验房
                } else {
                    if ("1".equals(objectMap.get("FitmentState").toString())) {
                        resmap.put("Wsubmit", 1);//提交验房
                    } else {
                        if ("1".equals(objectMap.get("CheckState").toString())) {
                            if ("1".equals(objectMap.get("IfCheckState").toString())) {
                                resmap.put("Wsubmit", 2);//再次提交
                            } else {
                                resmap.put("Wsubmit", 3);//已提交验房
                            }
                        } else if ("2".equals(objectMap.get("CheckState").toString())) {
                            resmap.put("Wsubmit", 4);//验房完成
                        }
                    }

                }


                log.info("obj====>" + resmap);
                reslist.add(resmap);
            }
            result.put(reslist);
        } catch (Exception e) {
            log.info("E====>" + e);
            result.putFailed("服务器繁忙...");
        }
        return result;
    }

    /**
     * 设置是否可以预销
     *
     * @param params
     * @return
     */
    public JsonResult updatehousingtt(Map<String, Object> params) {
        JsonResult result = new JsonResult();

        if ("0".equals(params.get("HouState").toString())) {
            params.put("FutTime", null);
        }
        log.info("paerams:" + params);
        try {
            if (mapper.updatehousingtt(params) > 0) {
                mapper.updatehousinginfo(params);
                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }

        } catch (Exception e) {
            log.info("E====>" + e);
            result.putFailed("服务器繁忙...");
        }

        return result;
    }

    /**
     * 设置是否可以预销
     *
     * @param params
     * @return
     */
    public JsonResult gettcmkt_housingByHouId(Map<String, Object> params) {
        JsonResult result = new JsonResult();

        log.info("paerams:" + params);
        try {
            result.put(mapper.gettcmkt_housingByHouId(params));
        } catch (Exception e) {
            log.info("E====>" + e);
            result.putFailed("服务器繁忙...");
        }

        return result;
    }


    /**
     * @param params
     * @return
     */
    public JsonResult updatehousingByHousId(Map<String, Object> params) {
        JsonResult result = new JsonResult();

        log.info("paerams:" + params);
        try {
            if (mapper.updatehousingByHousId(params) > 0) {
                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }
        } catch (Exception e) {
            log.info("E====>" + e);
            result.putFailed("服务器繁忙...");
        }
        return result;
    }

    /**
     * 装修计划已完成详情的查看房源
     *
     * @param paramas
     * @return
     */
    public JsonResult gethousingByHouId(List<Map<String, Object>> params) {
        JsonResult result = new JsonResult();
        log.info("params===>" + params);
        // List<Map<String,Object>> datalist = (List<Map<String, Object>>) paramas.get("maplist");
        //log.info("datalist===>"+datalist);
        List<Map<String, Object>> datamaplist = new ArrayList<Map<String, Object>>();
        for (Map<String, Object> objectMap : params) {
            Map<String, Object> getmap = mapper.getHougingbyhid(objectMap.get("HouId").toString());
            getmap.remove("HouId");
            getmap.putAll(objectMap);
            datamaplist.add(getmap);
        }
        result.put(datamaplist);
        return result;
    }

    //////////运营部

    /**
     * 根据位置查询小区
     **/
    public JsonResult getComName(Map<String, Object> params) {
        JsonResult result = new JsonResult();

        result.put(mapper.getComName(params));

        return result;
    }

    /**
     * 根据小区查询楼号
     **/
    public JsonResult getBuilding(Map<String, Object> params) {
        JsonResult result = new JsonResult();

        result.put(mapper.getBuilding(params));

        return result;
    }

    /**
     * 根据楼号查询室号
     **/
    public JsonResult getRoom(Map<String, Object> params) {
        JsonResult result = new JsonResult();

        result.put(mapper.getRoom(params));

        return result;
    }

    public JsonResult gethousingByids(Map<String, Object> params, Pager pager) {

        JsonResult result = new JsonResult();
        pager.setPageno(Integer.parseInt(params.get("pageno").toString()));
        pager.setPagesize(Integer.parseInt(params.get("pagesize").toString()));
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        log.info("pager====>" + pager);
        List<Map<String, Object>> maplist = (List<Map<String, Object>>) params.get("datalist");//获取id
        log.info("maplist====>" + maplist);
        List<String> houIdList = new ArrayList<String>();
        for (Map<String, Object> objectMap : maplist) {
            houIdList.add(objectMap.get("HouId").toString());//获取房源数组集合
        }
        params.put("houIdList", houIdList);
        if (maplist.size() == 0) {
            return pageUtils.getJsonResult(new ArrayList<Map<String, Object>>(), params, 0);
        }
        Integer cout = mapper.getCountByids(params);
        if (cout == 0) {
            return pageUtils.getJsonResult(new ArrayList<Map<String, Object>>(), params, cout);
        }
        List<Map<String, Object>> getCommunityList = mapper.gethousingByids(params);

        List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();

        log.info("maplist===>" + maplist);


        log.info("getCommunityList===>" + getCommunityList);

        for (int i = 0; i < maplist.size(); i++) {
            Map<String, Object> comList = maplist.get(i);
            for (int j = 0; j < getCommunityList.size(); j++) {
                Map<String, Object> map = getCommunityList.get(j);
                if (Objects.equals(MapUtils.getString(comList, "HouId"), MapUtils.getString(map, "HouId"))) {
                    comList.putAll(map);

                }
            }
            dataList.add(comList);
        }

        /*for (Map<String, Object> m : maplist) {
            Map<String, Object> mapw = new HashMap<String, Object>();
            mapw.put("CheckNoticeTime", m.get("CheckNoticeTime"));
            mapw.put("PlanId", m.get("PlanId"));
            mapw.put("PlansId", m.get("PlansId"));
            mapw.put("yuqi", m.get("yuqi"));
            mapw.put("HouId", m.get("HouId"));
            Map<String, Object> mapw1 = new HashMap<String, Object>();
            for (Map<String, Object> objectMap : getCommunityList) {
                mapw1.put("ComName", objectMap.get("ComName"));
                mapw1.put("Building", objectMap.get("Building"));
                mapw1.put("BudRom", objectMap.get("BudRom"));
            }
            mapw.put("ComName", mapw1.get("ComName"));
            mapw.put("Building", mapw1.get("Building"));
            mapw.put("BudRom", mapw1.get("BudRom"));
            resMapList.add(mapw);
        }*/
        result = pageUtils.getJsonResult(dataList, params, cout);

        return result;
    }


    /**
     * 新增房源配置 无限网络 等
     *
     * @param params
     * @return
     */
    public JsonResult saveHousingCon(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            List<Map<String, Object>> HouingCon = (List<Map<String, Object>>) params.get("HouingCon");//获取配置主表的数据
            List<Map<String, Object>> HouingConInfo = (List<Map<String, Object>>) params.get("HouingConInfo");//查询出配置详表
            List<Map<String, Object>> HouingConInfoWiFi = (List<Map<String, Object>>) params.get("HouingConInfoWiFi");//获取无限网阔电视
            for (Map<String, Object> houing : HouingCon) {// 新增配置主表
                if (mapper.saveHousingCon(houing) > 0) {
                    result.putSuccess("操作成功");
                    log.info("主表配置新增成功");
                }
            }
            String HouConId = null;
            for (Map<String, Object> coninfo : HouingConInfo) {
                Map<String, Object> uuid = utilsMapper.getUUID(new HashMap<String, Object>() {{
                    put("uuid", "HouConId");
                    put("database", Constants.DATA_SOURCE_TCMKT);
                    put("surface", "tcmkt_housing_con_info");
                }});
                if (uuid != null) {
                    int number = Integer.valueOf(uuid.get("uuid").toString());
                    HouConId = ComUtils.getLocalTrmSeqNum("Con", number);
                } else {
                    HouConId = ComUtils.getLocalTrmSeqNum("Con", 0);
                }

                // new Date()为获取当前系统时间
                coninfo.put("instm", df.format(new Date()));
                coninfo.put("HouConId", HouConId);
                if (mapper.saveHouingConInfo(coninfo) > 0) {
                    result.putSuccess("操作成功");
                    log.info("详表配置新增成功");
                }
            }
            String HouConInId = null;
            for (Map<String, Object> infowifi : HouingConInfoWiFi) {

                Map<String, Object> uuid = utilsMapper.getUUID(new HashMap<String, Object>() {{
                    put("uuid", "HouConInId");
                    put("database", Constants.DATA_SOURCE_TCMKT);
                    put("surface", "tcmkt_housing_con_internet");
                }});
                if (uuid != null) {
                    int number = Integer.valueOf(uuid.get("uuid").toString());
                    HouConInId = ComUtils.getLocalTrmSeqNum("HIn", number);
                } else {
                    HouConInId = ComUtils.getLocalTrmSeqNum("HIn", 0);
                }
                infowifi.put("instm", df.format(new Date()));
                infowifi.put("HouConInId", HouConInId);
                if (mapper.saveHouingConInfoWiFi(infowifi) > 0) {
                    result.putSuccess("操作成功");
                    log.info("无线网络 新增成功");
                }
            }
        } catch (Exception e) {
            log.error("E>>>>>" + e);
            result.putFailed("操作失败");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }


    /**
     * 新增新增厨房区域(管家验房的图片)
     *
     * @param params
     * @return
     */
    public JsonResult saveHouingKitchenFile(Map<String, Object> params) {

        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            List<Map<String, Object>> houImgList = (List<Map<String, Object>>) params.get("HouImg");//首先获得这个集合
            StringBuffer KitchenFile = new StringBuffer();

            for (Map<String, Object> houimg : houImgList) {
                KitchenFile.append(houimg.get("url").toString() + ",");//追加字符串
            }
            KitchenFile.deleteCharAt(KitchenFile.length() - 1);//去除字符串最后一个逗号
            log.info("KitchenFile>>>>>>>>>=" + KitchenFile);
            params.put("KitchenFile", KitchenFile.toString());

            if (mapper.saveHouingKitchenFile(params) > 0) {//新增新增厨房区域(管家验房的图片)
                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }
        } catch (Exception e) {
            log.error("saveHouingKitchenFile>>>>>" + e);
            result.putFailed("操作失败");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }

    public JsonResult gethousingKitchenFileByid(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        Map<String, Object> dataMap = mapper.gethousingKitchenFileByid(params);
        // List<String> imglist = (List<String>) dataMap.get("KitchenFile");//获取照片
        String[] Fileimgs;
        List<String> imlist = new ArrayList<String>();//转集合
        if (dataMap.get("KitchenFile") != null) {
            Fileimgs = dataMap.get("KitchenFile").toString().split(",");
            for (int i = 0; i < Fileimgs.length; i++) {
                imlist.add(Fileimgs[i].substring(Fileimgs[i].indexOf("http")));
            }
            dataMap.put("Fileimgs", imlist);
        } else {
            dataMap.put("Fileimgs", new ArrayList<String>());
        }
        result.put(dataMap);
        return result;
    }


    /**
     * 新增新增卫生间区域(管家验房的图片)
     *
     * @param params
     * @return
     */
    public JsonResult saveHouingToiletFile(Map<String, Object> params) {

        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            List<Map<String, Object>> houImgList = (List<Map<String, Object>>) params.get("HouImg");//首先获得这个集合
            StringBuffer KitchenFile = new StringBuffer();

            for (Map<String, Object> houimg : houImgList) {
                KitchenFile.append(houimg.get("url").toString() + ",");//追加字符串
            }
            KitchenFile.deleteCharAt(KitchenFile.length() - 1);//去除字符串最后一个逗号
            log.info("saveHouingToiletFile>>>>>>>>>=" + KitchenFile);
            params.put("ToiletFile", KitchenFile.toString());

            if (mapper.saveHouingToiletFile(params) > 0) {//新增新增厨房区域(管家验房的图片)
                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }
        } catch (Exception e) {
            log.error("saveHouingToiletFile>>>>>" + e);
            result.putFailed("操作失败");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }


    public JsonResult gethousingToiletFileByid(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        Map<String, Object> dataMap = mapper.gethousingToiletFileByid(params);
        // List<String> imglist = (List<String>) dataMap.get("KitchenFile");//获取照片
        String[] Fileimgs;
        List<String> imlist = new ArrayList<String>();//转集合
        if (dataMap.get("ToiletFile") != null) {
            Fileimgs = dataMap.get("ToiletFile").toString().split(",");
            for (int i = 0; i < Fileimgs.length; i++) {
                imlist.add(Fileimgs[i].substring(Fileimgs[i].indexOf("http")));
            }
            dataMap.put("Fileimgs", imlist);
        } else {
            dataMap.put("Fileimgs", new ArrayList<String>());
        }
        result.put(dataMap);
        return result;
    }


    /**
     * 新增新增卫生间区域(管家验房的图片)
     *
     * @param params
     * @return
     */
    public JsonResult saveHouingDiningFile(Map<String, Object> params) {

        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            List<Map<String, Object>> houImgList = (List<Map<String, Object>>) params.get("HouImg");//首先获得这个集合
            StringBuffer KitchenFile = new StringBuffer();

            for (Map<String, Object> houimg : houImgList) {
                KitchenFile.append(houimg.get("url").toString() + ",");//追加字符串
            }
            KitchenFile.deleteCharAt(KitchenFile.length() - 1);//去除字符串最后一个逗号
            log.info("saveHouingToiletFile>>>>>>>>>=" + KitchenFile);
            params.put("DiningFile", KitchenFile.toString());

            if (mapper.saveHouingDiningFile(params) > 0) {//新增新增厨房区域(管家验房的图片)
                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }
        } catch (Exception e) {
            log.error("saveHouingToiletFile>>>>>" + e);
            result.putFailed("操作失败");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }


    /**
     * 新增新增房源主图片 (管家验房的图片)
     *
     * @param params
     * @return
     */
    public JsonResult saveHouingShowImg(Map<String, Object> params) {

        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            List<Map<String, Object>> houImgList = (List<Map<String, Object>>) params.get("HouImg");//首先获得这个集合
            StringBuffer KitchenFile = new StringBuffer();

            for (Map<String, Object> houimg : houImgList) {
                KitchenFile.append(houimg.get("url").toString() + ",");//追加字符串
            }
            KitchenFile.deleteCharAt(KitchenFile.length() - 1);//去除字符串最后一个逗号
            log.info("saveHouingToiletFile>>>>>>>>>=" + KitchenFile);
            params.put("DiningFile", KitchenFile.toString());

            if (mapper.saveHouingShowImg(params) > 0) {//新增新增房源主图片 (管家验房的图片)
                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }
        } catch (Exception e) {
            log.error("saveHouingToiletFile>>>>>" + e);
            result.putFailed("操作失败");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }


    /**
     * 新增新增房源主图片 (管家验房的图片)
     *
     * @param params
     * @return
     */
    public JsonResult saveHouingHouseImg(Map<String, Object> params) {

        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            List<Map<String, Object>> houImgList = (List<Map<String, Object>>) params.get("HouImg");//首先获得这个集合
            StringBuffer KitchenFile = new StringBuffer();

            for (Map<String, Object> houimg : houImgList) {
                KitchenFile.append(houimg.get("url").toString() + ",");//追加字符串
            }
            KitchenFile.deleteCharAt(KitchenFile.length() - 1);//去除字符串最后一个逗号
            log.info("saveHouingToiletFile>>>>>>>>>=" + KitchenFile);
            params.put("DiningFile", KitchenFile.toString());

            if (mapper.saveHouingHouseImg(params) > 0) {//新增新增房源主图片 (管家验房的图片)
                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }
        } catch (Exception e) {
            log.error("saveHouingToiletFile>>>>>" + e);
            result.putFailed("操作失败");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }


    public JsonResult gethousingDiningFileByid(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        Map<String, Object> dataMap = mapper.gethousingDiningFileByid(params);
        // List<String> imglist = (List<String>) dataMap.get("KitchenFile");//获取照片
        String[] Fileimgs;
        List<String> imlist = new ArrayList<String>();//转集合
        if (dataMap.get("DiningFile") != null) {
            Fileimgs = dataMap.get("DiningFile").toString().split(",");
            for (int i = 0; i < Fileimgs.length; i++) {
                imlist.add(Fileimgs[i].substring(Fileimgs[i].indexOf("http")));
            }
            dataMap.put("Fileimgs", imlist);
        } else {
            dataMap.put("Fileimgs", new ArrayList<String>());
        }
        result.put(dataMap);
        return result;
    }


    public JsonResult gethousingShowImgByid(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        Map<String, Object> dataMap = mapper.gethousingShowImgByid(params);
        // List<String> imglist = (List<String>) dataMap.get("KitchenFile");//获取照片
        String[] Fileimgs;
        List<String> imlist = new ArrayList<String>();//转集合
        if (dataMap.get("ShowImg") != null) {
            Fileimgs = dataMap.get("ShowImg").toString().split(",");
            for (int i = 0; i < Fileimgs.length; i++) {
                imlist.add(Fileimgs[i].substring(Fileimgs[i].indexOf("http")));
            }
            dataMap.put("Fileimgs", imlist);
        } else {
            dataMap.put("Fileimgs", new ArrayList<String>());
        }
        result.put(dataMap);
        return result;
    }


    public JsonResult gethousingHouseImgByid(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        Map<String, Object> dataMap = mapper.gethousingHouseImgByid(params);
        // List<String> imglist = (List<String>) dataMap.get("KitchenFile");//获取照片
        String[] Fileimgs;
        List<String> imlist = new ArrayList<String>();//转集合
        if (dataMap.get("HouseImg") != null) {
            Fileimgs = dataMap.get("HouseImg").toString().split(",");
            for (int i = 0; i < Fileimgs.length; i++) {
                imlist.add(Fileimgs[i].substring(Fileimgs[i].indexOf("http")));
            }
            dataMap.put("Fileimgs", imlist);
        } else {
            dataMap.put("Fileimgs", new ArrayList<String>());
        }
        result.put(dataMap);
        return result;
    }


    public JsonResult getHousingConInfo(Map<String, Object> params) {
        JsonResult result = new JsonResult();

        List<Map<String, Object>> datalist = mapper.getHousingConInfo(params);
        List<Map<String, Object>> reslist = new ArrayList<Map<String, Object>>();
        for (Map<String, Object> list : datalist) {
            Map<String, Object> dataMap = mapper.getHouInfoFile(list.get("HouCon").toString());
            String[] Fileimgs;
            List<String> imlist = new ArrayList<String>();//转集合

            if (dataMap.get("HouInfoFile") != null) {
                Fileimgs = dataMap.get("HouInfoFile").toString().split(",");
                for (int i = 0; i < Fileimgs.length; i++) {
                    imlist.add(Fileimgs[i].substring(Fileimgs[i].indexOf("http")));
                }
                list.put("HouInfoimgs", imlist);
            } else {
                list.put("HouInfoimgs", new ArrayList<String>());
            }
            reslist.add(list);
        }
        result.put(reslist);

        return result;
    }

    /**
     * 新增房间的图片(管家验房的图片)
     *
     * @param params
     * @return
     */
    public JsonResult saveHouingInfoFile(Map<String, Object> params) {

        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            List<Map<String, Object>> houImgList = (List<Map<String, Object>>) params.get("HouImg");//首先获得这个集合
            StringBuffer KitchenFile = new StringBuffer();

            for (Map<String, Object> houimg : houImgList) {
                KitchenFile.append(houimg.get("url").toString() + ",");//追加字符串
            }
            KitchenFile.deleteCharAt(KitchenFile.length() - 1);//去除字符串最后一个逗号
            log.info("saveHouingInfoFile>>>>>>>>>=" + KitchenFile);
            params.put("HouInfoFile", KitchenFile.toString());
            if (mapper.saveHouingInfoFile(params) > 0) {//
                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }
        } catch (Exception e) {
            log.error("saveHouingInfoFile>>>>>" + e);
            result.putFailed("操作失败");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }


    public JsonResult getocuntHouging(Map<String, Object> params) {
        JsonResult result = new JsonResult();

        Map<String, Object> dataMap = new HashMap<String, Object>();

        dataMap.put("housingcon", mapper.getcouhousing(params));//公共区域
        dataMap.put("infocon", mapper.getCounthousing_info(params));//房间的

        result.put(dataMap);
        return result;
    }


    /**
     * 修改房源详表的验房时间
     *
     * @param params
     * @return
     */
    public JsonResult updateAccepted(Map<String, Object> params) {

        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            params.put("Accepted", df.format(new Date()));//当前时间
            if (mapper.updateAccepted(params) > 0) {//
                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }
        } catch (Exception e) {
            log.error("saveHouingInfoFile>>>>>" + e);
            result.putFailed("操作失败");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }


    public JsonResult gethousingByState(Map<String, Object> params) {

        JsonResult result = new JsonResult();

        try {

            result.put(mapper.gethousingByState(params));

        } catch (Exception e) {
            log.error("saveHouingInfoFile>>>>>" + e);
            result.putFailed("操作失败");
        }
        return result;
    }

}
